<?php
namespace plugin\entity_manager\src;

/**
 * The MIT License
 *
 * Copyright (c) <year> <copyright holders>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author Damiano Ciarla
 * @package plugin
 * @subpackage entity_manager/class/abstract
 */
abstract class AbstractEntity
{
	/**
	 * Lista delle proprietà della entity
	 *
	 * @var array
	 */
	private $__property;

	/**
	 * Metodo che restituisce un array con tutte le propietà intrinseche della entity
	 *
	 * @return array
	 */
	public function getProperty()
	{
		if ($this->__property === null)
		{
			$this->__property = EntityProperty::getInstance($this)->getProperty();
		}
		else
		{
			$this->__property = EntityProperty::getInstance($this)->updatePropertyValues($this->__property);
		}

		return $this->__property;
	}

	/**
	 * Metodo che valida la entity in base ai valori settati
	 *
	 * @param boolean $getErrors Se impostata su true restituisce l'array con la lista degli errori
	 *
	 * @return boolean|array
	 */
	public function validate($getErrors = false)
	{
		return EntityProperty::getInstance($this)->validate($getErrors);
	}

	/**
	 * Metodo popola automaticamente i campi matchando la chiave dell'array con il nome
	 * del campo esplicitato nell'oggetto istanziato
	 *
	 * @param array $params Lista dei parametri da popolare
	 */
	public function populate(array $params)
	{
		$property = $this->getProperty();

		foreach($params as $field => $value)
		{
			if(array_key_exists($field, $property['FIELDS']))
				$this->$field = $value;
		}
	}

	public function __get($name)
	{
		$property = $this->getProperty();

		if(!array_key_exists($name, $property['ENTITY']['GET']))
			throw new exception\EntityManagerException($params, $code);

		$dataRelationship = $property['ENTITY']['GET'][$name];

		$name = '\\entity_manager\\model\\' . $name;
		
		$instanceObj = new $name();
		$instanceObj->$dataRelationship['TO_FIELD'] = $property['FIELDS'][$dataRelationship['SELF_FIELD']]['value'];

		return EntityManager::find($instanceObj)->execute();
	}
}
?>
